package com.tanqing.ddd.web.controller;

import com.tanqing.ddd.domain.command.*;
import com.tanqing.ddd.domain.domain.DomainException;
import com.tanqing.ddd.domain.entity.Plate;
import com.tanqing.ddd.domain.event.EventQueue;
import com.tanqing.ddd.domain.utils.DateUtils;
import com.tanqing.ddd.web.domain.CommandInvoker;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class CheckInController {
    @Resource
    private CheckinCommandHandle checkinCommandHandle;

    @Resource
    private CheckOutCommandHandle checkOutCommandHandle;

    @Resource
    private FeeCommandHandle feeCommandHandle;

    @Resource
    private NotifyPayCommandHandle notifyPayCommandHandle;
    @Resource
    private CommandInvoker commandInvoker;

    /**
     * 入场
     */
    @RequestMapping(value = "checkin", method = RequestMethod.POST)
    public void checkin() {
        Plate plate = new Plate();
        plate.setPlateNumber("渝A123456");
        commandInvoker.invoke(dto -> {
            CheckinCommand checkinCommand = new CheckinCommand();
            checkinCommand.setPlate(plate);
            checkinCommand.setDate(DateUtils.formDate("2024-01-04 10:00:20", DateUtils.DATETIME_FORMAT));
            return checkinCommandHandle.handle((EventQueue) dto, checkinCommand);
        });
    }

    /**
     * 查询费用
     */
    @RequestMapping(value = "queryfee", method = RequestMethod.POST)
    public void queryfee() {
        Plate plate = new Plate();
        plate.setPlateNumber("渝A123456");
        commandInvoker.invoke(dto -> {
            FeeCommand feeCommand = new FeeCommand();
            feeCommand.setPlate(plate);
            feeCommand.setDate(DateUtils.formDate("2024-01-04 10:00:20", DateUtils.DATETIME_FORMAT));
            BigDecimal handle = feeCommandHandle.handle(feeCommand);
//            return feeCommandHandle.handle((EventQueue) dto, feeCommand);
            return handle;
        });
    }

    /**
     * 支付
     */
    @RequestMapping(value = "pay", method = RequestMethod.POST)
    public void pay() {
        Plate plate = new Plate();
        plate.setPlateNumber("渝A123456");
        commandInvoker.invoke(dto -> {
            NotifyPayCommand notifyPayCommand = new NotifyPayCommand();
            notifyPayCommand.setPlate(plate);
            notifyPayCommand.setAmount(new BigDecimal("1"));
            notifyPayCommand.setPayTime(DateUtils.formDate("2024-01-04 10:00:20", DateUtils.DATETIME_FORMAT));
            try {
                return notifyPayCommandHandle.handle((EventQueue) dto, notifyPayCommand);
            } catch (DomainException e) {
                throw new RuntimeException(e);
            }
        });
    }

    /**
     * 出场
     */
    @RequestMapping(value = "checkout", method = RequestMethod.POST)
    public void checkout() {
        Plate plate = new Plate();
        plate.setPlateNumber("渝A123456");
        commandInvoker.invoke(dto -> {
            CheckOutCommand checkOutCommand = new CheckOutCommand();
            checkOutCommand.setPlate(plate);
            checkOutCommand.setDate(DateUtils.formDate("2024-01-04 10:00:20", DateUtils.DATETIME_FORMAT));
            return checkOutCommandHandle.hadnle((EventQueue) dto, checkOutCommand);
        });
    }
}
